package com.msc.cili.service.subscribe.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.msc.cili.repository.dao.subscribe.SubscribeMapper;
import com.msc.cili.repository.entity.subscribe.SubRecord;
import com.msc.cili.repository.entity.subscribe.Subscribe;
import com.msc.cili.repository.entity.user.User;
import com.msc.cili.service.subscribe.SubRecordService;
import com.msc.cili.service.subscribe.SubscribeService;
import com.msc.cili.service.subscribe.bo.SubscribeBo;
import com.msc.cili.service.user.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;


/**
 * @author masc
 * @date 2022/6/25 0:01
 * <p></p>
 */
@Service
public class SubscribeServiceImpl extends ServiceImpl<SubscribeMapper, Subscribe> implements SubscribeService {
    @Resource
    private SubscribeMapper subscribeMapper;
    @Resource
    private UserService userService;
    @Resource
    private SubRecordService subRecordService;

    @Override
    public IPage<Subscribe> getAllScribeList(Integer typeId, int pageIndex, int pageSize) {
        Page<Subscribe> page = new Page(pageIndex, pageSize);
        QueryWrapper<Subscribe> queryWrapper = new QueryWrapper<>();
        if (typeId != null) {
            queryWrapper.lambda().eq(Subscribe::getTypeId, typeId);
        }
        queryWrapper.lambda().eq(Subscribe::getStatus, 1);


        IPage<Subscribe> subscribeIPage = subscribeMapper.selectPage(page, queryWrapper);

        return subscribeIPage;
    }

    @Override
    public boolean buySubscribe(SubscribeBo subscribeVo) {
        //加事务

        // 根据id拿到详情，价格信息
        Subscribe subscribe = subscribeMapper.selectById(subscribeVo.getId());

        //拿到用户信息 判断之前是否买过
        Long userId =111L ;   // ShiroUtil.getProfile().getId();
        String name = "";   //ShiroUtil.getProfile().getUsername();
        QueryWrapper<SubRecord> queryWrapperRecord = new QueryWrapper<>();
        queryWrapperRecord.lambda().eq(SubRecord::getUserId, userId).eq(SubRecord::getSubscribeId, subscribeVo.getId());
        SubRecord record = subRecordService.getOne(queryWrapperRecord);

        // 扣减CC 判断是否余额足够
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getId, userId);
        User userInfo = new User(); // userService.getOne(queryWrapper);
        BigDecimal subtract = userInfo.getCc().subtract(subscribe.getPrice());
        //余额大于零，且没有购买记录
        if (subtract.compareTo(new BigDecimal("0")) > 0 && null == record) {
            // 次数增加1 扣减CC
            Integer subTimes = subscribe.getSubTimes();
            //将次数加一操作
            subscribe.setSubTimes(subTimes + 1);
            boolean a = this.saveOrUpdate(subscribe);
//            扣减CC
            userInfo.setCc(subtract);
            boolean b = true;//userService.saveOrUpdate(userInfo);

            // 创建购买记录
            SubRecord subRecord = new SubRecord();
            subRecord.setSubscribeId(subscribeVo.getId());
            subRecord.setTitle(subscribe.getTitle());
            subRecord.setUserId(userId);
            subRecord.setCostCc(subscribe.getPrice());
            subRecord.setFirstPicture(subscribe.getFirstPicture());
            subRecord.setNickname(userInfo.getNickname());
            subRecord.setUsername(userInfo.getUsername());
            subRecord.setStatus(1);
            subRecord.setCreateTime(LocalDateTime.now());
            boolean c = subRecordService.saveOrUpdate(subRecord);
            return a && b && c;
        }
        return false;
    }
}
